const app = getApp();
var common = require('./../../utils/common');
Page({
  data: {
    calendar: [],
    year: '',
    month: '',
    time: '',
    inTime: '',
    outTime: '',
    date: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
    langTime: 0,
    inDay: '',
    inMonth: '',
    inWeek: '',
    outDay: '',
    outMonth: '',
    outWeek: '',
    timeSign: 0,
    nowTime: common.toDayTime()
  },
  // 获取固定日期的星期
  weekDay: function (time) { 
    let week = new Date(time).getDay();
    let str = '';
    switch (week) {
      case 0 :
        week = '周日';
        break;
      case 1:
        week = '周一';
        break;
      case 2:
        week = '周二';
        break;
      case 3:
        week = '周三';
        break;
      case 4:
        week = '周四';
        break;
      case 5:
        week = '周五';
        break;
      case 6:
        week = '周六';
        break;
    }
    return week;
  },
  // 日历数据改变
  dateChange () {
    var inTime = this.data.inTime;
    var outTime = this.data.outTime;
    this.setData({
      inDay: inTime.slice(8, 10),
      inMonth: inTime.slice(5, 7),
      inWeek: this.weekDay(inTime),
      outDay: outTime.slice(8, 10),
      outMonth: outTime.slice(5, 7),
      outWeek: this.weekDay(outTime)
    });
  },
  // 读取日期缓存
  getTime () {
    var inTime = my.getStorageSync({
      key: 'inTime', // 缓存数据的key
    }).data;
    var outTime = my.getStorageSync({
      key: 'outTime', // 缓存数据的key
    }).data;
    var langTime = (new Date(outTime) - new Date(inTime)) / 86400000;
    this.setData({
      inTime: inTime,
      outTime: outTime,
      langTime: langTime
    });
    this.dateChange();
  },
  // 获取制定日期第二天的时间
  nextDay (time) {
    var times = new Date(new Date(time).getTime() + 86400000);
    var year = times.getFullYear();
    var month = times.getMonth() < 10 ? '0' + (times.getMonth() + 1) : (times.getMonth() + 1);
    var day = times.getDate() < 10 ?  '0' + times.getDate() : times.getDate();
    return year + '-' + month + '-' + day;
  },
  // 日历初始化
  dataInit (setYear, setMonth) {
    // 当前时间/传入的时间
    let now = setYear ? new Date(setYear, setMonth) : new Date();
    let year = setYear || now.getFullYear();
    // 构建某日数据时使用
    let obj = {};
    // 需要遍历的日历数组数据
    let dateArr = [];
    // 需要的格子数，为当前星期数+当月天数
    let arrLen = 0;
    // 该月数值未加1
    let month = setMonth || now.getMonth() + 1;
    // 该月加1的数值，如果大于11，则为下年，月份重置为1 
    //目标月1号对应的星期
    let startWeek = new Date(year + '-' + (month < 10 ? '0' + month : month) + '-01').getDay();
    // console.log(year + '-' + (month + 1) + '星期' + startWeek);
    //获取目标月有多少天
    let dayNums = common.getCountDays(year + '-' + (month < 10 ? '0' + month : month) + '-01');
    let num = 0;
    // 计算当月需要的格子数 = 当前星期数+当月天数
    // 第一天从周日开始，所以需要加1 
    arrLen = startWeek * 1 + dayNums * 1;
    for (let i = 0; i < arrLen; i++) {
      if (i >= startWeek) {
        num = i - startWeek + 1;
        obj = {
          isToday: year + '-' + (month < 10 ? '0' + month : month) + '-' + (num < 10 ? '0' + num : num),
          dateNum: num,
          weight: 5
        }
      } else {
        obj = {};
      }
      dateArr[i] = obj;
    };
    return dateArr;
  },
  // 点击日期
  timeClick (e) {
    // 设置日期缓存
    var inTime = this.data.inTime;
    var outTime = this.data.outTime;
    var langTime = 0;
    var eTime = e.currentTarget.dataset.sign;
    if (e.currentTarget.dataset.noclick == 1) {
      return false;
    }
    if (this.data.timeSign == 0) {
      inTime = eTime;
      // outTime = this.nextDay(eTime);
      outTime = "";
      langTime = (new Date(outTime) - new Date(inTime)) / 86400000;
      this.setData({
        timeSign: 1
      });
    } else if (this.data.timeSign == 1 && new Date(inTime) >= new Date(eTime)) {
      inTime = eTime;
      outTime = "";
      // outTime = this.nextDay(eTime);
      langTime = (new Date(outTime) - new Date(inTime)) / 86400000;
      this.setData({
        timeSign: 1
      });
    } else {
      inTime = inTime;
      outTime = eTime;
      langTime = (new Date(outTime) - new Date(inTime)) / 86400000;
      this.setData({
        timeSign: 0
      });
    };
    this.setData({
      inTime: inTime,
      outTime: outTime,
      langTime: langTime
    });
    this.dateChange();
  },
  // 点击确认
  makeTime () {
    if (this.data.inTime == "") {
      my.alert({
        content: '请选择入住日期'
      })
      return false;
    } else if (this.data.outTime == "") {
      my.alert({
        content: '请选择离店日期'
      })
      return false;
    }
    my.setStorageSync({
      key: 'inTime',
      data: this.data.inTime
    });
    my.setStorageSync({
      key: 'outTime',
      data: this.data.outTime
    });
    my.navigateBack();
  },
  onLoad(options) {
    my.showLoading();
    var nowTime = new Date();
    var year = nowTime.getFullYear();
    var month = nowTime.getMonth();
    var time = [];
    var timeArray = {};
    for (var i = 0 ; i < 6; i++) {
      year = month + 1 > 12 ? year + 1 : year;
      month = month + 1 > 12 ? 1 : month + 1;
      time = this.dataInit(year, month);
      // timeArray.push(time);
      timeArray[year + '年' + (month < 10 ? '0' + month : month) + '月'] = time;
    };
    this.setData({
      calendar: timeArray,
      year: nowTime.getFullYear(),
      month: nowTime.getMonth()
    });
    my.hideLoading();
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
    this.getTime();
  },
  onHide() {
    // 页面隐藏 
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息 
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
})
